package com.learn.animation;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.ux.fragment.ProductPickerFragment;
import com.ux.widget.BottomMenuItemView;
import com.ux.widget.ResideMenuView.BottomMenuItemViewListener;
import com.ux.widget.helper.ResideMenuDragHelper.DragDirection;

public class MainActivity extends BaseActivity {
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		resideMenuView.updateMainView(getSupportFragmentManager(), new PlaceholderFragment());
		
		initSalesMenuBottomItems(getApplicationContext());
	}
	
	@Override
	public void onBackPressed() {
		//super.onBackPressed();
		resideMenuView.openSlideUpView(false);
		return;
	}
	
	private BottomMenuItemView menuNew, menuAdd, menuManual, menuPay;
	void initSalesMenuBottomItems(Context context) {
		// add bottom menu item
		menuNew = new BottomMenuItemView(context, 0);
		menuNew.setText("New");
		menuNew.setBadgeText("+");
		menuNew.setIcon(R.drawable.ic_menu_shopping_cart);
		resideMenuView.addBottomMenuItem(menuNew);

		menuAdd = new BottomMenuItemView(context, 1);
		menuAdd.setText("Add");
		menuAdd.setBadgeText("0");
		menuAdd.setIcon(R.drawable.ic_menu_shopping_cart);
		resideMenuView.addBottomMenuItem(menuAdd);
		
		menuManual = new BottomMenuItemView(context, 2);
		menuManual.setText("Manual");
		menuManual.setBadgeText("✎");
		menuManual.setIcon(R.drawable.ic_menu_shopping_cart);
		resideMenuView.addBottomMenuItem(menuManual);
		
		menuPay = new BottomMenuItemView(context, 3);
		menuPay.setText("Pay");
		menuPay.setIcon(R.drawable.ic_menu_receipt);
		resideMenuView.addBottomMenuItem(menuPay);
		
		registerMenuItemClickEvent();
		
		resideMenuView.updateSlideUpView(getSupportFragmentManager(), new ProductPickerFragment());
	}
	private boolean isProductCatalogShowed = false;
	private void registerMenuItemClickEvent() {
		resideMenuView.addBottomMenuItemViewListener(new BottomMenuItemViewListener() {
			
			@Override
			public void onMenuItemClicked(BottomMenuItemView menuItem) {
				switch (menuItem.getId()) {
				case 0:// new
					Toast.makeText(getApplicationContext(), "Do you want to start New Sales?", Toast.LENGTH_SHORT).show();
					break;
					
				case 1:// add product from Product Catalog
					if (!isProductCatalogShowed) {
						resideMenuView.openSlideUpView(true);
						menuAdd.setEnableView(false);
					} else {
						resideMenuView.openSlideUpView(false);
						menuAdd.setEnableView(true);
					}
					
					isProductCatalogShowed = !isProductCatalogShowed;
					break;
					
				case 2:// manual add Product
					Toast.makeText(getApplicationContext(), "Add Manual Product", Toast.LENGTH_SHORT).show();
					break;
					
				case 3:// pay
					Toast.makeText(getApplicationContext(), "Process Pay", Toast.LENGTH_SHORT).show();
					break;

				default:
					break;
				}
			}
		});
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public class PlaceholderFragment extends Fragment {
		
		private View viewAnimate;
		private Button buttonTest;

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			viewAnimate = inflater.inflate(R.layout.fragment_main, container, false);
			buttonTest = (Button) viewAnimate.findViewById(R.id.buttonTest);
			buttonTest.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					//test();
					initSalesMenuBottomItems(getActivity());
					
					// slide up panel
					resideMenuView.openSlideUpView(true);
				}
			});
			
			viewAnimate.findViewById(R.id.buttonOpen).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					BottomMenuItemView menuAdd = new BottomMenuItemView(getActivity(), 0);
					menuAdd.setText("Add");
					resideMenuView.addBottomMenuItem(menuAdd);
					
					resideMenuView.openMenu(DragDirection.DRAG_DIRECTION_FROM_LEFT);
				}
			});
			viewAnimate.findViewById(R.id.buttonClose).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					//sResideMenuView.closeOpenedResideMenu();
					resideMenuView.openMenu(DragDirection.DRAG_DIRECTION_FROM_RIGHT);
				}
			});
			
			return viewAnimate;
		}
		
		int count = 0;
		float dis = 0;
		Handler mHandler = new Handler();
		private Runnable countRunnable = new Runnable() {
			@Override
			public void run() {
				/*if (count < 30) {
					count += 1;
					scaleCount = scaleCount - 0.01f;
					test();
				} else {
					count = 0;
				}*/
				animate();
			}
		};
		private float scaleCount = 1.0f;
		boolean scaleUp = false;
		
		private void test() {
			animate();
		}
		private float _scale = 1.0f;
		private void animate() {
			// setScale
	        Log.i("mapp", "[INF] scale="+scaleCount);
	        if (scaleCount < 0.75f) {
	        	return;
	        }
			viewAnimate.setScaleX(_scale);
			viewAnimate.setScaleY(_scale);
			
			_scale -= 0.0008;
			
			mHandler.postDelayed(countRunnable, 10);
		}
		
	}
	
}
